﻿@{
    ViewBag.Title = "任务待办";
    Layout = "~/Views/Shared/_indexBase1.5.cshtml";
    if (false)
    {
        <script src="~/LiberUI/Scripts/jquery-2.0.3.js"></script>
        <script src="~/LiberUI/Scripts/jquery.ba-resize.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Base.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Controllers.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Action.js"></script>
        <script src="~/SiteUI/BaseScripts/site.js"></script>
    }
    <link href="~/SiteUI/Base.css" rel="stylesheet" />
    <link href="~/SiteUI/Task/Task.css" rel="stylesheet" />
    <link href="~/SiteUI/BaseScripts/pikaday.css" rel="stylesheet" />

}
@*临时容器，放置需动态展现的自定义页面部件*@
<div id="tmp">
    @RenderPage("/SiteUI/wgBase.cshtml")
    @RenderPage("/SiteUI/Task/wgTask.cshtml")
</div>
@section HeadCnt{
    <div class="MiddleWraper">
        <h2 class="index_Title">@ViewBag.Title</h2>
        <a class="SubIndex active" href="#">我的任务</a>
        <a class="SubIndex" href="#">看板</a>
        <a class="SubIndex" href="/Task/Manage">任务管理</a>
    </div>
}

@section HeadBar{
    <div id="Tools" class="LeftWraper">
        <div id="Add">新增任务</div>
    </div>
}

@section LeftNavi{
    <div id="frmFastNew">
        <h4 class="required">任务内容：</h4>
        <div id="txbCnt" placeholder="任务内容" contenteditable="true" class="HtmlEditor"></div>
        <input id="txbProposer" placeholder="提出人" />
        <input id="tmProposeTime" placeholder="接入时间" />
        <input id="tmDeadline" placeholder="完成时限" />
        <input id="numTimeValue" type="number" placeholder="可结算工作量"/>
        <input id="numTimeCost" type="number" placeholder="实际耗时" />
        <div id="slcType" class="Selector required">任务类别</div>
        <div id="slcSub" class="Selector required">关联子系统</div>
        <div id="slcProj" class="Selector required">所属项目</div>
        <div id="ratImportance"></div>
        <div id="btnAdd">保存</div>
    </div>
    <h2>今日待办：</h2>
    <div id="lstTodo">

    </div>
    <br />
    <h2>我的任务：</h2>
    <div id="lstTask">

    </div>
    <span id="numTaskSum"></span>
}

<div id="frmNewTask">

</div>
<div id="pnlTaskInfo">

</div>

<script type="text/javascript">
    FastForm = {};
    Env.page = 0;
    Env.hasMore = false;

    function onAddClick() {
        FastForm.Reset();
        $("#frmFastNew").slideDown();
        FastForm.txbContent.focus();
        Env.mnuProposers.Viewer($("#txbProposer"));
        wgTask.mnuTaskType.Viewer(FastForm.slcType);
        wgTask.mnuSubSystem.Viewer(FastForm.slcSubsystem);
        Env.mnuUserProjs.Viewer(FastForm.sclProj);
    };

    function fastNew() {
        if (FastForm.checker.Check()) {
            new AjaxHelper('post', '/Task/New', FastForm.val(), fastNewEnd);
            FastForm.Reset();
        }
    };

    function fastNewEnd(rtn) {
        if (rtn.IsPositive) {
            if (rtn.Data.Delay >= 0) {
                Env.lstTodo.Append(rtn.Data);
            }
            else {
                Env.lstTask.Append(rtn.Data);
            }
        }
        else {
            //TODO:改为系统标准的错误提示方式
            console.log('新增任务时发生错误：', rtn);
        }
    };

    function getTodoEnd(rtn) {
        if (rtn.IsPositive) {
            Env.lstTodo.Append(rtn.Data.Tasks);
        }
        else if (Env.lstTodo.Data().length == 0) {
            
        }
        else {

        }
    };

    function getTaskEnd(rtn) {
        Env.isLoading = false;
        if (rtn.IsPositive) {
            Env.lstTask.Append(rtn.Data.Tasks);
            if (rtn.Data.SUM > 0) {
                $("#numTaskSum").text(Env.lstTask.Data().length + '/' + rtn.Data.SUM);
                if (rtn.Data.SUM > Env.lstTask.Data().length) {
                    Env.page = Env.page + 1;
                    Env.hasMore = true;
                    Env.lstScler.on('getBottom', onLstTaskScroll);
                }
                else {
                    Env.hasMore = false;
                    Env.lstScler.off('getBottom', onLstTaskScroll);
                }
            }
        }
        else if(Env.lstTask.Data().length==0){
            
        }
        else {

        }
    };

    function onTodoSlcted(item) {
        Env.lstTask.Select(-1);
        Env.CItem = item;
        $("#frmFastNew").slideUp(Env.lstScler.Reset);
        wgTask.LoadTask(item.Data);
    };
    function onTaskSlcted(item) {
        Env.lstTodo.Select(-1);
        Env.CItem = item;
        $("#frmFastNew").slideUp(Env.lstScler.Reset);
        wgTask.LoadTask(item.Data);
    };

    function onLstTaskScroll() {
        if (Env.hasMore == true&&!Env.isLoading) {
            Env.isLoading = true;
            new AjaxHelper('get', '/Task/MyTask', { page: Env.page }, getTaskEnd);
        }
    };

    function updateTask(tsk) {
        if (Env.CItem.Data.IDStr == tsk.IDStr) {
            Env.CItem.Update(tsk);
        }
    };

    $(function () {
        //初始化快速新增表单
        FastForm.form = $("#frmFastNew").hide();
        FastForm.txbContent = new HtmlEditor($("#txbCnt"));
        FastForm.txbProposer = $("#txbProposer");
        FastForm.ratImportance = new Rating($("#ratImportance"), { DefaultDescribe: '(-重急程度)' });
        FastForm.tmDeadline = new Pikaday({
            field: $("#tmDeadline")[0]
        });
        FastForm.tmProposeTime = new Pikaday({
            field: $("#tmProposeTime")[0]
        });
        FastForm.numTimeValue = $("#numTimeValue");
        FastForm.numTimeCost = $("#numTimeCost");
        FastForm.slcType = new Controller($("#slcType"));
        FastForm.slcType.af_setValue = function (_type) {
            if (_type && _type.Value) {
                this._member.$rootElm.text(_type.Value);
            }
            else {
                this._member.$rootElm.text('任务类别');
            }
        };
        FastForm.slcType.af_getValue = function (v) {
            if (v && v.Value) {
                v.Value = this._member.$rootElm.text();
            }
            else {
                v = null;
            }
            return v;
        };
        FastForm.slcSubsystem = new Controller($("#slcSub"));
        FastForm.slcSubsystem.af_setValue = function (_sub) {
            if (_sub && _sub.Value) {
                this._member.$rootElm.text(_sub.Value);
            }
            else {
                this._member.$rootElm.text('关联子系统');
            }
        };
        FastForm.slcSubsystem.af_getValue = function (v) {
            if (v && v.Value) {
                v.Value = this._member.$rootElm.text();
            }
            else {
                v = null;
            }
            return v;
        };

        FastForm.slcProj = new ProjViewForSlctor($("#slcProj"));
 
        FastForm.Reset = function () {
            FastForm.txbContent.val('');
            FastForm.txbProposer.val('');
            FastForm.tmProposeTime.setDate('');
            FastForm.tmDeadline.setDate('');
            FastForm.numTimeValue.val('');
            FastForm.numTimeCost.val('');
            $("#tmProposeTime").val('');
            $("#tmDeadline").val('');
            FastForm.slcType.val({});
            FastForm.slcSubsystem.val({});
            FastForm.ratImportance.val('');
            Env.mnuUserProjs.Viewer(FastForm.slcProj);
        };
        FastForm.val = function (_newValue) {
            if (ObjHelper.IsEmpty(_newValue)) {
                var n = {};
                n.cnt = FastForm.txbContent.val();
                n.proposer = FastForm.txbProposer.val();
                n.proposeTime = FastForm.tmProposeTime.toString();
                n.deadline = FastForm.tmDeadline.toString();
                n.typeId = FastForm.slcType.val().IDStr;
                n.projId = FastForm.slcProj.val().IDStr;
                n.subSystemId = FastForm.slcSubsystem.val().IDStr;
                n.importance = FastForm.ratImportance.val();
                n.timeValue = FastForm.numTimeValue.val();
                n.timeCost = FastForm.numTimeCost.val();
                return n;
            }
        };

        FastForm.btnAdd = new Button($("#btnAdd")).on('click', fastNew);
        FastForm.checker = new Validater({ CheckOnBlur: false })
                                            .IsDate($("#tmProposeTime"), '', '抱歉，您填写的日期格式无法识别')
                                            .NoEmpty(FastForm.txbContent, '', '请填写任务内容的简要描述')
                                            .NoEmpty(FastForm.ratImportance, '', '请评估此任务的重急程度')
                                            .NoEmpty(FastForm.slcType, '', '请选择此任务的分类')
                                            .NoEmpty(FastForm.slcSubsystem, '', '请选择此任务所关联的子系统');

        //设置所属项目的可选值
        Env.mnuUserProjs.Menu().Append('@ViewBag.TaskProjs'.ToJSON());

        //处理常用登记人历史记录
        Env.CommonProposers = '@ViewBag.CommonProposers'.ToJSON();
        Env.mnuProposers.Menu().Append(Env.CommonProposers);

        //加载任务类型和关联子系统选项
        wgTask.lstTypes.Append('@ViewBag.TaskTypes'.ToJSON());
        wgTask.lstSubSystems.Append('@ViewBag.SubSystems'.ToJSON());

        //初始化今日待办列表
        Env.lstTodo = new List($("#lstTodo"), { ShowHead: false, ShowFoot: false, Selectble: true });  //今日待办列表
        Env.lstTodo.af_makeView = wgTask.MakeTaskCard;
        Env.lstTodo.on('itemSlcted', onTodoSlcted);

        //初始化我的任务列表
        Env.lstTask = new List($("#lstTask"), { ShowHead: false, ShowFoot: false, Selectble: true });  //我的处理中任务
        Env.lstTask.af_makeView = wgTask.MakeTaskCard;
        Env.lstTask.on('itemSlcted', onTaskSlcted);

        //布局任务视图
        wgTask.vTaskView.appendTo($("#pnlTaskInfo"));

        //设置页面主工具条按钮功能
        $("#Add").click(onAddClick);

        //重载任务内容发生变化时调用的函数
        wgTask.vf_onTaskUpdated = updateTask;

        //加载今日待办
        new AjaxHelper('get', '/Task/UserTodo', {}, getTodoEnd);

        //加载我的任务（未完成的）
        new AjaxHelper('get', '/Task/UserTask', { page: Env.page }, getTaskEnd);
    })
</script>


